package ConwaysGameOfLife;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JOptionPane;

/**
 *
 * @author Bryan
 */

//This is the primary container for all UI modules
public class MainDisplay extends javax.swing.JFrame {


    private final WorldControlMediator mediator;
    private final int bufferSpace = 20;

    public MainDisplay() {
        this.mediator = null;

    }

    MainDisplay(WorldControlMediator mediator) {

        this.mediator = mediator;
        setFeel();
        initComponents();
        componentSetup();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jSlider1 = new javax.swing.JSlider();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton1.setText("jButton1");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(0, 0, 0));
        setPreferredSize(new java.awt.Dimension(1000, 1000));
        setResizable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1021, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 640, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void setFeel() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
    }
    
    public void componentSetup(){
        
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screenSize.width / 2 - getWidth() / 2, screenSize.height / 2 - getHeight() / 2);

        //Creating new components
        WorldSettings worldSettings = new WorldSettings(mediator);
        JDesktopInternal primaryDesktopPane = new JDesktopInternal(mediator);
        ColorPallete worldColorPallete = new ColorPallete(this, true, mediator);
        ColorPallete brushColorPallete = new ColorPallete(this, true, mediator);

        //Setting the color pallete colors
        worldColorPallete.setCurrentColor(mediator.getWorldColor());
        brushColorPallete.setCurrentColor(mediator.getBrushColor());

        //adding components to their appropriate containers
        this.getContentPane().add(primaryDesktopPane);
        primaryDesktopPane.add(worldSettings);

        //Adjusting the size and location of many components
        this.setSize(1007, 829);
        primaryDesktopPane.setBounds(0, 0, this.getWidth() - 4, this.getHeight() - 25);
        primaryDesktopPane.setBackground(Color.red);
        worldColorPallete.setLocation(getX() / 2 + worldColorPallete.getWidth() / 2, getY() / 2 + worldColorPallete.getHeight() / 2);
        brushColorPallete.setLocation(getX() / 2 + brushColorPallete.getWidth() / 2, getY() / 2 + brushColorPallete.getHeight() / 2);
        worldSettings.setLocation(getWidth() - (worldSettings.getWidth() + bufferSpace * 2), bufferSpace);
       
        //registering classes with the mediator for easy access
        mediator.setBrushColorPallete(brushColorPallete);
        mediator.setWorldColorPallete(worldColorPallete);
        mediator.setWorldDisplay(primaryDesktopPane);
        mediator.setWorldSettings(worldSettings);

        //final color update
        mediator.updateColors();
        
        //Finally, making all components visible
        worldSettings.setVisible(true);
        setVisible(true);
        primaryDesktopPane.setVisible(true);
        
        JOptionPane.showMessageDialog(rootPane, "Hello User, \n\n"
                + "Welcome to a Java implementation of Conway's Game of Life! \n\n"
                + "A few pointers to get you started:\n\n"
                + "  - Once a world is started you may draw new cells anywhere on the board by clicking and dragging\n"
                + "  - The \"Random Spawn\" check box will add lots of random cells when you start a game\n"
                + "  - \"Spawn Density\" determines the density of randomly spawned cells\n"
                + "  - \"Clear Simulation\" will kill all cells on the board, but not end the simulation\n"
                + "  - Some settings cannot be changed while a game is at play, end the game to change them\n\n"
                + "Hit start to begin.\n\n"
                + "Enjoy your Game of Life!", "Welcome!", WIDTH);
     

    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables
}
